package com.tuyan.aixiang.activity.qr;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.widget.ImageView;

import com.gyf.immersionbar.ImmersionBar;
import com.tuyan.aixiang.R;
import com.tuyan.aixiang.activity.web.WebActivity;
import com.tuyan.aixiang.common.AppData;
import com.tuyan.aixiang.common.BaseActivity;
import com.tuyan.aixiang.common.BasePresenter;
import com.tuyan.aixiang.common.Constant;
import com.tuyan.aixiang.model.YsDevice;
import com.tuyan.aixiang.util.Log;
import com.tuyan.aixiang.util.ToastUtil;
import com.tuyan.aixiang.util.YSUtil;
import com.uuzuche.lib_zxing.activity.CaptureFragment;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.videogo.openapi.EZOpenSDK;

import butterknife.BindView;
import butterknife.OnClick;

public class NewQrScanActivity extends BaseActivity<BasePresenter> {

    private static final String TAG = "NewQrScanActivity";

    @BindView(R.id.iv_light)
    ImageView ivLight;

    private Activity context;

    @Override
    public void bindView() {
        super.bindView();
        EZOpenSDK.setAccessToken(AppData.getYsToken());
        context = this;
        ImmersionBar.with(this).keyboardEnable(true)  //解决软键盘与底部输入框冲突问题
//                .flymeOSStatusBarFontColor(R.color.textDark)
                .statusBarDarkFont(true, 0.2f)
                .fitsSystemWindows(true)
                .statusBarColor(R.color.colorPure)
                .init();
        findViewById(R.id.iv_back).setOnClickListener(v -> finish());
        // block this
        findViewById(R.id.iv_edit).setOnClickListener(v -> {
            eidtClick();
        });
        /**
         * 执行扫面Fragment的初始化操作
         */
        CaptureFragment captureFragment = new CaptureFragment();
        // 为二维码扫描界面设置定制化界面
        CodeUtils.setFragmentArgs(captureFragment, R.layout.fragment_qr);
        captureFragment.setAnalyzeCallback(new CodeUtils.AnalyzeCallback() {
            @Override
            public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
                Log.d(TAG, "result = " + result);
//                ToastUtil.makeShort("扫描成功");

                YsDevice device = YSUtil.getYsDevice(result);
                if (null == device) {
                    ToastUtil.makeShort("无法识别此设备");
//                    return false;
                } else {
                    Log.d(TAG, device.toString());
                    YSUtil.gotoDeviceConfig(context, device);
                }

                finish();
            }

            @Override
            public void onAnalyzeFailed() {
                Log.d(TAG, "failed");
            }
        });
        /**
         * 替换我们的扫描控件
         */
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_my_container, captureFragment).commit();

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_new_qr;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    private boolean lightEnable = false;

    @OnClick(R.id.iv_light)
    public void toggleLight() {
        lightEnable = !lightEnable;
        CodeUtils.isLightEnable(lightEnable);
    }

    public void eidtClick() {
        Intent intent = new Intent(this, WebActivity.class);
        intent.putExtra("url", Constant.MANAUL_ADD_DEVICE);
        startActivity(intent);
        finish();
    }
}
